package com.example.pattern10_facadepattern.door.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 切面定义
 * @ EnableConfigurationProperties的作用是将 @ConfigurationProperties 注解的对象转成Bean对象。
 * @ ConfigurationProperties的作用是将配置文件转换成类对象，便于修改或者获取值。
 */
@Configuration
@ConditionalOnClass(StarterService.class)
@EnableConfigurationProperties(StarterServiceProperties.class)
public class StarterAutoConfigure {

    @Autowired
    private StarterServiceProperties properties;

    //@ConditionalOnMissingBean作用：
    // 判断当前需要注入Spring容器中的bean的实现类是否已经含有，有的话不注入，没有就注入
    @Bean
    @ConditionalOnMissingBean
    //当itstack.door.enabled = true的时候注入容器
    @ConditionalOnProperty(prefix = "codinginn.door", value = "enabled", havingValue = "true")
    StarterService starterService() {
        return new StarterService(properties.getUserStr());
    }

}